﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Book_of_Shadow_Script
{
    abstract class TextFormatBuilder
    {
        public abstract void FromString(string str);
        public abstract void FromString(string[] str);
    }
    class TextFormat_CodeTable : TextFormatBuilder
    {
        int code;
        char ch;
        public int Code { get { return code; } }
        public char Character { get { return ch; } }
        public TextFormat_CodeTable()
        {
        }
        public TextFormat_CodeTable(string str)
        {
            FromString(str);
        }
        public override void FromString(string[] str)
        {
            throw new NotImplementedException();
        }
        public override void FromString(string str)
        {
            int p1 = str.IndexOf('=');
            if (p1 + 1 < str.Length)
            {
                ch = str[p1 + 1];
                code = Convert.ToInt32(str.Substring(0, p1), 16);
            }
            else
                throw new IllegalFormatException(p1);
        }
    }
    class TextFormat_Book_of_Shadow : TextFormatBuilder
    {
        int id, addr, type, count;
        string[] strs,strs_t,strs_f;
        public int ID { get { return id; } }
        public int Address { get { return addr; } }
        public int Count { get { return count; } }
        public int FirstAddr { get { return saddr[0]; } }
        public int Type { get { return type; } }
        int[] len,saddr;
        public TextFormat_Book_of_Shadow(string[] s)
        {
            FromString(s);
        }
        public override void FromString(string str)
        {
            throw new NotImplementedException();
        }
        public string Text(int i)
        {
            string ret;
            ret = this.strs_f[i].Trim();//有润色取润色
            if (ret == null || ret.Trim() == String.Empty) ret = strs_t[i];//否则取翻译
            if (ret == null || ret.Trim() == String.Empty) ret = strs[i];//否则取原文
            return ret;
        }
        public override void FromString(string[] s)
        {
            string tmp = s[0];
            tmp = tmp.Trim();
            int p1=tmp.IndexOf(':');
            if (p1 != tmp.Length-1 || p1<=0) throw new IllegalFormatException("冒号", tmp.Length);
            tmp = tmp.Substring(0, p1);
            id = Convert.ToInt32(tmp);
            string[] tmps = s[1].Split(new char[]{','});
            if (tmps.Length != 3) throw new IllegalFormatException("line 2 comma", id);
            addr = Convert.ToInt32(tmps[0],16);
            type = Convert.ToInt32(tmps[1],16);
            count = Convert.ToInt32(tmps[2]);

            strs=new string[count];
            strs_t=new string[count];
            strs_f=new string[count];
            len=new int[count];
            saddr=new int[count];
            int j=2;
            for (int i = 0; i < count; i++)
            {
                tmps = s[j].Split(new char[] { ',' });
                if (tmps.Length != 2) throw new IllegalFormatException("comma line", id);
                saddr[i] = Convert.ToInt32(tmps[0], 16);
                len[i] = Convert.ToInt32(tmps[1]);
                j++;
                strs[i] = s[j].Substring(4);
                j++;
                strs_t[i] = s[j].Substring(4);
                j++;
                strs_f[i] = s[j].Substring(4);
                j += 2;
            }
        }
    }
    class TextFormat_WQSG : TextFormatBuilder
    {
        int addr, len;
        string str;
        public int Address { get { return addr; } }
        public int Length { get { return len; } }
        public string String { get { return str; } }

        public TextFormat_WQSG(string str)
        {
            FromString(str);
        }
        public override void FromString(string[] str)
        {
            throw new NotImplementedException();
        }
        public override void FromString(string fullstr)
        {
            int p1 = fullstr.IndexOf(',');
            if (p1 < 0) throw new IllegalFormatException(-1);

            string tmp = fullstr.Substring(0, p1);
            addr = Convert.ToInt32(tmp,16);
            fullstr = fullstr.Substring(p1+1);
            p1 = fullstr.IndexOf(',');
            if (p1 < 0) throw new IllegalFormatException(-2);

            tmp = fullstr.Substring(0,p1);
            len = Convert.ToInt32(tmp);
            len++;
            str = fullstr.Substring(p1 + 1);
        }
    }
}
